package com.example.online_forum.services.impl;

import com.example.online_forum.common.AppResult;
import com.example.online_forum.common.ResultCode;
import com.example.online_forum.dao.BoardMapper;
import com.example.online_forum.exception.AppException;
import com.example.online_forum.model.Board;
import com.example.online_forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Autowired
    BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        if(num <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new AppException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //返回数据库查询的结果
        List<Board> result= boardMapper.selectByNum(num);
        return result;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //非空校验
        if(id <= 0 || id == null){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new AppException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //通过数据库查询对应的板块帖子数量信息
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null){
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString() + ", board id=" + id);
            throw new AppException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新板块内帖子数量
        Board updateboard = new Board();
        updateboard.setId(board.getId());
        updateboard.setArticleCount(board.getArticleCount() + 1);
        int row = boardMapper.updateByPrimaryKeySelective(updateboard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于 1");
            throw new AppException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        if(id <= 0 || id == null){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new AppException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board =  boardMapper.selectByPrimaryKey(id);
        //返回结果
        return board;
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //非空校验
        if(id == null || id <= 0 ){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new AppException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ", board id =" + id);
            throw new AppException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        //判断-1之后小于0
        if(updateBoard.getArticleCount() < 0){
            updateBoard.setArticleCount(0);
        }
        //调用dao
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于 1");
            throw new AppException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
